package com.ruanyun.modular.downWord;

import freemarker.template.Template;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.Map;

public class ExportWordUtils {



    public static File createDoc(Map<?, ?> dataMap, Template template) {
        File f = new File("D:/testWord"+Math.random()*1000+".doc");
        try {
            //这个地方不能使用FileWriter因为需要指定编码类型否则生成的word文档会因为有无法识别的编码而无法打开
            Writer w = new OutputStreamWriter(new FileOutputStream(f), "utf-8");
            template.process(dataMap, w);
            w.close();
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
        return f;

    }
}
